
import { defineStore } from "pinia";
import { login, getUserInfo, doUppwd } from "@/api/index.ts"
import { doWxlogin, getWxPhone, doWxInformation } from "@/api/wx.js"
import useRouter from "@/hooks/useRouter"
import useImStore from "./useImStore";
import useTencentIm from '@/hooks/useTencentIm';
const router = useRouter();
const tencentIm = useTencentIm();
type UserState = {
	token ?: string,
	userId ?: number,
	head ?: string,
	realname ?: string,
	uid ?: string,
	username ?: string,
	uuid ?: string,
	blance ?: string;
	brithday ?: string;
	jobs ?: string;
	my_ask_nums ?: number;
	my_case_nums ?: number;
	my_collect_nums ?: number;
	nickname ?: string;
	tel ?: string;
	isWxLogining ?: boolean;
	introduct ?: string; // 用户简介
	desc ?: string; // 用户描述
}
const useUserStore = defineStore("user-store", {
	persist: {
		// 仅在客户端使用
		paths: ['userId', 'token', 'head', 'realname', 'uid', 'username', 'uuid', 'nickname']
	},
	state: () : UserState => ({
		token: "", // 用户登录后的token
		userId: 0, // 用户id，用于区分不同的用户
		head: "",
		realname: "",
		uid: "",
		username: "",
		uuid: "",
		blance: "",
		brithday: "",
		jobs: "",
		my_ask_nums: 0,
		my_case_nums: 0,
		my_collect_nums: 0,
		nickname: "",
		tel: "",
		isWxLogining: false,
		introduct: "",
		desc: "",
	}),

	getters: {

	},

	actions: {
		setIsWxLogining(isWxLogining) {
			this.$patch({
				isWxLogining: isWxLogining,
			});
		},
		setToken(token, userId) {
			this.$patch({
				token: token,
				userId: userId,
				uid: userId,
			});
			this.getUserInfo();
		},
		doUpPwd(data : Partial<any>) {
			doUppwd(data).then(() => {
				this.$patch({
					token: "",
					userId: "",
				});
				uni.$wdToast.show("密码修改成功,请重新登录"); // 调用提示方法，显示提示信息。
				setTimeout(() => {
					router.push("/commonPage/login/login"); // 登录成功后跳转到首页。
				}, 1000)

			});
		},

		getUserInfo() {
			const imStore = useImStore();
			getUserInfo().then(async (res : any) => {
				this.$patch({
					...res,
				});
				setTimeout(() => {
					imStore.login();
				}, 1000)
			});
		},
		async wxSaveInfo(data) {
			return await doWxInformation(data);
		},
		async wxPhone(data) {
			return await getWxPhone(data);
		},
		wxLogin() {
			if (!this.isWxLogining) {
				this.isWxLogining = true;
				uni.login({
					provider: 'weixin', //使用微信登录
					success: (loginRes) => {
						doWxlogin({
							code: loginRes.code, //获取到的code值
						}).then(res => {
							if (!res.token) {
								// uni.showAuthPopup(res.openid)
								uni.$emit('on-show-auth-popup', res.openid);
							} else {
								this.isWxLogining = false;
								this.setToken(res.token, res.user_id);
								try {
									console.log("刷新页面");
									uni.pageReLoad();
								} catch (error) {
									console.log(error);
								}
								// router.switchTab('/pages/index/index');
							}

						});
					}
				});
			}

			// getWxPhone
		},
		doLogin(loginForm : Partial<any>) {
			login({
				phone: loginForm.phone,
				password: loginForm.password,
			}).then((res : any) => {
				this.$patch({
					token: res.token,
					userId: res.user_id,
				});
				router.switchTab("/pages/index/index"); // 登录成功后跳转到首页。
				this.getUserInfo();
				uni.$wdToast.show("登陆成功"); // 调用提示方法，显示提示信息。
			})
		},
	},
});

export default useUserStore;